package com.superdry.activity;

import org.json.JSONObject;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Checkable;
import android.widget.EditText;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.ab.util.AbDialogUtil;
import com.ab.util.AbStrUtil;
import com.ab.util.AbToastUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.drysurper.R;
import com.google.gson.Gson;
import com.superdry.global.SApplication;
import com.superdry.utils.CheckLogin;
import com.superdry.utils.GlobalConst;
import com.superdry.utils.SPHelper;

public class AdviseActivity extends AbActivity{

	View back;
	View advise_ok;
	EditText advise_et;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setAbContentView(R.layout.aty_advise);
		
		SApplication.getInstance().addActivity(this);
		initView();
	}
	
	public void initView(){
		
		back = findViewById(R.id.goback_advise);
		advise_ok = findViewById(R.id.advise_ok);
		advise_et = (EditText) findViewById(R.id.advise_et);
		
		back.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		advise_ok.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(CheckLogin.isLogin(AdviseActivity.this))
					commitAdvise();
			}
		});
	}
	
	public void commitAdvise(){
		
		String content = advise_et.getText().toString();
		if(AbStrUtil.isEmpty(content)){
			AbToastUtil.showToast(AdviseActivity.this, "您输入的为空！");
			return;
		}
			
		
		AbHttpUtil http = AbHttpUtil.getInstance(AdviseActivity.this);
		AbRequestParams params = new AbRequestParams();
		params.put("messageContent", content);
		http.post(GlobalConst.COMMIT_ADVISE + "&userID=" + SPHelper.getUserInfo(AdviseActivity.this).getUserID() , params, new AbStringHttpResponseListener() {
			
			@Override
			public void onStart() {
				Log.i("http", "request begin");
			}
			
			@Override
			public void onFinish() {
				Log.i("http", "request finished");
			}
			
			@Override
			public void onFailure(int statusCode, String content, Throwable error) {
				Log.i("http", "request failure");
				AbToastUtil.showToast(AdviseActivity.this, content);
			}
			
			@Override
			public void onSuccess(int statusCode, String content) {
				Log.i("http", "request successful");
				JSONArray ja = JSONArray.parseArray(content);
				com.alibaba.fastjson.JSONObject jo = JSON.parseObject(ja.get(0).toString());
				int i = (Integer) jo.get("i");
				if(i == 1){
					AbToastUtil.showToast(AdviseActivity.this, "提交成功！");
					finish();
				}else{
					AbToastUtil.showToast(AdviseActivity.this, "提交失败，请稍后重试！");
				}
					
			}
		});
	}
	
}
